//
//  UserCenterController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/2/28.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "UserCenterController.h"
#import "UserHeaderView.h"
#import "BeeGroupListObj.h"
#import "InviteObj.h"

@interface UserCenterController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *mainView;
    UserHeaderView *userHeader;
    
    UIButton *logoutBtn;
}

@end

@implementation UserCenterController

-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void)viewDidDisappear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_ShowTabbar object:nil];
    
    [self refreshView];
    
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_HiddeTabbar object:nil];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.largeTitle.hidden = YES;
    
    [self buildView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)buildView
{
    userHeader = [[UserHeaderView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, NavBarHeight + 100)];
    
    mainView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT) style:UITableViewStyleGrouped];
    mainView.separatorStyle = UITableViewCellSeparatorStyleNone;
    mainView.alwaysBounceVertical = NO;
    mainView.backgroundColor = [UIColor clearColor];
    mainView.tableHeaderView = userHeader;
    mainView.delegate = self;
    mainView.dataSource = self;
    [self.view addSubview:mainView];
    
    
    logoutBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.addBackGoundColor(UIColorFromRGB(0xF0F0F0));
        make.addTitleColor(FBEE_COLOR_THEME,UIControlStateNormal);
        make.addFont([UIFont systemFontOfSize:15.0f]);
        make.addTitleColor(FBEE_COLOR_INFO,UIControlStateHighlighted);
        make.addTarget(self,@selector(logoutAction));
        make.layer.cornerRadius = 6;
        make.addTitle(NSLocalizedString(@"uCenterLogout", nil));
        make.setFrame(CGRectMake(15, 0, IPHONEWIDTH - 30, 50));
        make.bottom = IPHONEHEIGHT - TabbarHeight - 45;
        [self.view addSubview:make];
    }];
    
    
}

-(void)refreshView
{
    logoutBtn.hidden = NO;
    
    if (![UserInfoManager sharedManager].isLogin) {
        logoutBtn.hidden = YES;
    }
    
    [userHeader refreshHeader];
    
    [mainView reloadData];
}

#pragma mark - TableView Cell

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"CELLID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *title = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addTextColor(FBEE_COLOR_TEXT).addFont([UIFont systemFontOfSize:18.0f]);
            make.setFrame(CGRectMake(20, (70 - 20)/2, IPHONEWIDTH - 80, 20));
            make.tag = 111;
        }];
        [cell addSubview:title];
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 9, 16)];
        arrow.image = [UIImage imageNamed:@"cell_arrow_r"];
        arrow.right = IPHONEWIDTH - 15;
        arrow.top = (70 - 16)/2;
        [cell addSubview:arrow];
        
        UILabel *info = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.addTextColor(FBEE_COLOR_TEXT).addFont([UIFont systemFontOfSize:18.0f]).addTextAlignment(NSTextAlignmentRight);
            make.setFrame(CGRectMake(0, (70 - 20)/2, 80, 20));
            make.tag = 222;
            make.right = arrow.left - 10;
        }];
        [cell addSubview:info];
    }
    
    UILabel *title = [cell viewWithTag:111];
    UILabel *info = [cell viewWithTag:222];
    title.text = info.text = @"";
    
    NSString *titleStr = @"";
    
    switch (indexPath.row) {
        case 0:
        {
            titleStr = NSLocalizedString(@"uCenterWallet", nil);
            break;
        }
        case 1:
        {
            titleStr = NSLocalizedString(@"uCenterTopic", nil);
            info.text = [UserInfoManager sharedManager].topicCount;
            break;
        }
        case 2:
        {
            titleStr = NSLocalizedString(@"uCenterAbout", nil);
            break;
        }
        default:
            break;
    }
    
    title.text = titleStr;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 2) {
        [[JumpManager sharedManager] jumpToAboat];
        return;
    }
    
    if (![UserInfoManager sharedManager].isLogin) {
        [[JumpManager sharedManager] jumpToLogin];
        return;
    }
    
    if (indexPath.row == 0) {
        [[JumpManager sharedManager] jumpToMyWallet];
        return;
    }
    
    if (indexPath.row == 1) {
        [[JumpManager sharedManager] jumpToMyTopic];
        return;
    }
    

    
}

#pragma maerk - logout

-(void)logoutAction
{
    
    __weak id sel = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您真的要退出登录吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UserInfoManager sharedManager] logOut];
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_LoginOut object:nil];
        [sel refreshView];
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:actionCancel];
    [alert addAction:actionOk];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

@end
